<?php
require_once 'databaseConnection.php';

try {
    $dbConn = connectToDatabase();

    // 查询数据库中的房间数据
    $sql = "SELECT number, status, roomType, cleanliness, cleaner, qualified, sellable, lastCleaned FROM roomstatus";
    $result = $dbConn->query($sql);

    // 生成房间数组
    $rooms = [];

    if ($result->rowCount() > 0) {
        while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
            $room = [
                'number' => $row['number'],
                'status' => $row['status'],
                'roomType' => $row['roomType'],
                'cleanliness' => $row['cleanliness'],
                'cleaner' => $row['cleaner'],
                'qualified' => ($row['qualified'] == '合格') ? true : false,
                'sellable' => ($row['sellable'] == '可出售') ? true : false,
                'lastcleaned' => $row['lastCleaned']
            ];
            array_push($rooms, $room);
        }
    }

    // 关闭数据库连接
    $dbConn = null;

    // 将房间数组转换为 JSON 格式并返回给前端
    echo json_encode($rooms);
} catch (PDOException $e) {
    echo "连接数据库时发生错误: " . $e->getMessage();
}
?>